#include <stdio.h>

int fact(int n)
{
    int i = 0, k = 0;
    while (n / 10 != 0)
    {
        i = n % 10;
        k = k * 10 + i;
        n /= 10;
    }
    k = k * 10 + n;
    return k;
}

int isHws(int n){
    int y = 0, k = n;
    while (k>0)
    {
        y = y * 10 + k % 10;
        k /= 10;
    }

    if(y==n)
        return 1;
    else
        return 0;
}


int main(int argc, char *argv[]) {

    int n, k=0, sum = 0, count = 1;
    scanf("%d", &n);
    while (n--)
    {
        k = 0, sum = 0, count = 1;
        scanf("%d", &k);
        sum = k + fact(k);
        while(!isHws(sum)){
            if(count>8){
                printf("0\n");
                break;
            }
            sum = sum + fact(sum);
            count++;
        }

        if(count<=8){
            printf("%d\n", count);
        }
        
    }

    return 0;
}